<?php

namespace App\Jobs;

use App\Models\ImGroup;
use App\Models\ImGroupRelate;
use App\Models\ImQueueErrorLogs;
use App\Services\Huanxin\GroupServices;
use App\Services\Huanxin\UserServices;
use App\Services\RongCloud\GroupService;
use App\Services\Netease\GroupServices as NetGroupServices;
use App\Services\RongCloud\UserService as RongUserService;
use App\Services\Netease\UserServices as NeteaseUserService;
use App\Services\Aliyun\Video\VideoServices;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class GroupTask implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $data = null;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data = null)
    {
        //
        $this->data = $data;
    }

    /**
     * @desc 同步创建的群组
     * @return bool
     * @author zhouzhou
     * @time 2021/12/24 16:30
     */
    public function handle()
    {
        // redis的数据
        $data = $this->data;
        if (!empty($data))
        {
            try{
                $imGroup = new ImGroup();
                $imGroupRelate = new ImGroupRelate();

                $phrase = new PhraseBuilder(13, '0123456789');
                $CaptchaBuilder = new CaptchaBuilder(null, $phrase);
                $groupId = $data['group_id'];
                $member = $data['member_id'];
                $groupName = $data['group_name'];
                $userId = $data['user_id'];
                $type = $data['type']; // 1环信 2融云 3网易云
                $imGroupInfo = $imGroup->where(['im_gid' => $groupId])->first();
                $memberList = !empty($member) ?  explode(",",$member) : [];

                // 同步融云 网易云
                if ($type == 1)
                {
                    $captcha = $CaptchaBuilder->getPhrase();
                    $rongGid = "16".$captcha;

                    // 同步融云
                    $groupRongServices= new GroupService();
                    $groupRongServices->create($userId, $rongGid, $groupName);
                    if ($memberList)
                    {
                        $groupRongServices->joinGroup($memberList, $rongGid, $groupName);
                    }

                    // 插入关联
                    $imGroupRelate->insertRelateGroup($groupId, $rongGid, 2);

                    // 同步网易云
                    $groupNetServices= new NetGroupServices();
                    $audit = ($imGroupInfo['is_allow'] == 1) ? true : false; // true 不需要审核， false 需要审核
                    $groupServiceInfo = $groupNetServices->createGroup($groupName,strval($userId), $memberList, '欢迎入群', '',  $imGroupInfo['description'], 0, $audit, 0);
                    $neteaseId = $groupServiceInfo['data']['tid'];

                    // 插入关联
                    $imGroupRelate->insertRelateGroup($groupId, $neteaseId, 3);
                }

                // 同步im和网易云
                if ($type == 2)
                {
                    // 同步im群组
                    $imGroupInfo = $imGroup->where(['im_gid' => $groupId])->first();
                    $groupServices= new GroupServices();

                    $public = ($imGroupInfo['open'] == 1) ? true : false; // 1公开，2私有
                    $allow = ($imGroupInfo['is_allow'] == 1) ? true : false; // 0：不允许，1：允许
                    $audit = ($imGroupInfo['is_allow'] == 1) ? true : false; // true 不需要审核， false 需要审核
                    $groupServiceInfo = $groupServices->createGroup($imGroupInfo['name'],$imGroupInfo['description'], $public, $allow, $audit, strval($imGroupInfo['user_id']));
                    $gid = $groupServiceInfo['data']['data']['groupid'];

                    // 同步群组用户
                    $service['usernames'] = $memberList;
                    if ($memberList)
                    {
                        $groupServices->addChatGroupsMembers($gid, $service);
                    }

                    // 插入环信关联
                    $imGroupRelate->insertRelateGroup($groupId, $gid, 1);

                    // 同步网易云
                    $groupNetServices= new NetGroupServices();
                    $audit = ($imGroupInfo['is_allow'] == 1) ? true : false; // true 不需要审核， false 需要审核
                    $groupServiceInfo = $groupNetServices->createGroup($groupName,strval($userId), $memberList, '欢迎入群', '',  $imGroupInfo['description'], 0, $audit, 0);

                    $neteaseId = $groupServiceInfo['data']['tid'];

                    // 插入网易云关联
                    $imGroupRelate->insertRelateGroup($groupId, $neteaseId, 3);
                }

                // 同步im和融云
                if ($type == 3)
                {
                    // 同步im群组
                    $groupServices= new GroupServices();

                    $public = ($imGroupInfo['open'] == 1) ? true : false; // 1公开，2私有
                    $allow = ($imGroupInfo['is_allow'] == 1) ? true : false; // 0：不允许，1：允许
                    $audit = ($imGroupInfo['is_allow'] == 1) ? true : false; // true 不需要审核， false 需要审核
                    $groupServiceInfo = $groupServices->createGroup($imGroupInfo['name'],$imGroupInfo['description'], $public, $allow, $audit, strval($imGroupInfo['user_id']));
                    $gid = $groupServiceInfo['data']['data']['groupid'];

                    // 同步群组用户
                    $service['usernames'] = $memberList;
                    if ($memberList)
                    {
                        $groupServices->addChatGroupsMembers($gid, $service);
                    }

                    // 插入环信关联
                    $imGroupRelate->insertRelateGroup($groupId, $gid, 1);

                    $captcha = $CaptchaBuilder->getPhrase();
                    $rongGid = "16".$captcha;

                    // 同步融云
                    $groupRongServices= new GroupService();
                    $groupRongServices->create($userId, $rongGid, $groupName);
                    $memberList = explode(',',$member);
                    if ($memberList)
                    {
                        $groupRongServices->joinGroup($memberList, $rongGid, $groupName);
                    }

                    // 插入融云关联
                    $imGroupRelate->insertRelateGroup($groupId, $rongGid, 2);
                }
            } catch (\Exception $e)
            {
                $imQueueErrorLogs = new ImQueueErrorLogs();
                $imQueueErrorLogs->insertQueueError(json_encode(['data'=>$data]), 2, $e->getMessage());
            }

        }

    }
}
